/** \file TraceMgr.cc
 * Definitions for Trace Manager class.
 *
 * (c) Copyright Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: Trace.cc,v 1.3 2008/06/19 06:48:08 semihc Exp $
 *
 *
 */

#include "TraceMgr.hh"
#include <stdio.h>
#include <iostream>
#include "Trace.hh"
#include "DynTrace.hh"
#include "Debug.hh"

using namespace std;
using namespace TC;


TraceMgr::TraceMgr()
{
  // Allocate reasonable inital value for average stack depth
  m_stack.reserve(128); 
}

TraceMgr::~TraceMgr()
{
  m_stack.clear();
}


TraceMgr& TraceMgr::GetInstance()
{
  static TC_THREAD_LOCAL TraceMgr* tm = 0;
  if(!tm)
    tm = new TraceMgr;
  return *tm;
}


void TraceMgr::printStack() const
{
  if(m_stack.empty())
    return;

  TC_LOG_INFO("Current trace stack:");
  FILE* f = Debug::GetSinkFile();

  for(int i=m_stack.size()-1; i>=0; --i) {
    fprintf(f,"%2.2d %s@%s:%d\n",
            i,
            m_stack[i]->m_func,
            m_stack[i]->m_file,
            m_stack[i]->m_line);
  }
    
  fflush(f);
}


ostream& TC::operator<<(ostream& os, const CodeContext& ctx)
{
  os << ctx.m_func << '@' << ctx.m_file << ':' << ctx.m_line;
  return os;
}
